package cn.stylefeng.guns.modular.recy.controller;
/*
 */

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import cn.stylefeng.guns.core.shiro.ShiroKit;
import cn.stylefeng.roses.core.base.controller.*;
import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.*;
import cn.stylefeng.guns.modular.recy.service.*;
import org.springframework.beans.factory.annotation.*;
import cn.stylefeng.roses.core.reqres.response.*;
import cn.stylefeng.guns.modular.recy.entity.*;
import java.io.*;
import cn.stylefeng.guns.core.common.page.*;
import com.baomidou.mybatisplus.core.metadata.*;
import com.baomidou.mybatisplus.core.conditions.*;
import cn.stylefeng.guns.modular.recy.vo.*;
import cn.stylefeng.guns.modular.recy.utils.*;
import cn.hutool.core.bean.*;
import java.util.stream.*;
import cn.stylefeng.guns.modular.recy.warpper.*;
import com.baomidou.mybatisplus.core.conditions.query.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.*;
import java.util.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.ui.*;

@Controller
@RequestMapping({ "/commandOrder" })
public class CommandOrderController extends BaseController
{
    private String PREFIX;
    @Autowired
    private CommandOrderService commandOrderService;
    @Autowired
    private BinsService binsService;

    @Autowired
    private WasteTypeService wasteTypeService;
    
    public CommandOrderController() {
        this.PREFIX = "/modular/recy/commandOrder/";
    }
    
    @RequestMapping({ "" })
    public String index() {
        return this.PREFIX + "commandOrder.html";
    }
    
    @RequestMapping({ "/add" })
    public String commandOrderAdd(Model model) {
        List<Bins> binsList = this.binsService.list();
        model.addAttribute("binsList",binsList);
        return this.PREFIX + "commandOrder_add.html";
    }
    
    @RequestMapping({ "/edit" })
    public String commandOrderUpdate(@RequestParam("id") final String id) {
        return this.PREFIX + "commandOrder_edit.html";
    }
    
    @RequestMapping({ "/detail/{id}" })
    @ResponseBody
    public ResponseData detail(@PathVariable final String id) {
        final CommandOrder commandOrder = (CommandOrder)this.commandOrderService.getById((Serializable)id);
        return (ResponseData)ResponseData.success(commandOrder);
    }
    
    @RequestMapping({ "/list" })
    @ResponseBody
    public Object list(final CommandOrder commandOrderQuery) {
        final QueryWrapper<CommandOrder> commandOrderQueryWrapper = ControllerUtil.createQuery(commandOrderQuery, this.getHttpServletRequest());
        final Page<CommandOrder> page = (Page<CommandOrder>)LayuiPageFactory.defaultPage();
        final IPage<CommandOrder> page2 = (IPage<CommandOrder>)this.commandOrderService.page((IPage)page, (Wrapper)commandOrderQueryWrapper);
        final List<CommandOrderVO> commandOrderVOList = EntityBind.bind(page2.getRecords(), CommandOrderVO.class);
        final List<Map<String, Object>> list = commandOrderVOList.stream().map(commandOrder -> BeanUtil.beanToMap(commandOrder)).collect(Collectors.toList());
        page2.setRecords((List)new CommandOrderWrapper(list).wrap());
        return LayuiPageFactory.createPageInfo(page2);
    }
    
    @RequestMapping({ "/addItem" })
    @ResponseBody
    public Object add(final CommandOrder commandOrder) {
        commandOrder.setIsRead(0);
        commandOrder.setIsFinish(0);
        commandOrder.setCreateTime(new Date());
        commandOrder.setCreateUserId(ShiroKit.getUser().getId());
        this.commandOrderService.save(commandOrder);
        return CommandOrderController.SUCCESS_TIP;
    }
    
    @RequestMapping({ "/delete" })
    @ResponseBody
    public Object delete(@RequestParam final String id) {
        this.commandOrderService.removeById((Serializable)id);
        return CommandOrderController.SUCCESS_TIP;
    }
    
    @RequestMapping({ "/editItem" })
    @ResponseBody
    public Object update(final CommandOrder commandOrder) {
        this.commandOrderService.updateById(commandOrder);
        return CommandOrderController.SUCCESS_TIP;
    }
    
    @RequestMapping(value = { "/delItems" }, method = { RequestMethod.POST })
    @ResponseBody
    public Object delItem(@RequestBody final String[] ids) {
        this.commandOrderService.removeByIds((Collection)Arrays.asList(ids));
        return CommandOrderController.SUCCESS_TIP;
    }
    
    @RequestMapping({ "/show" })
    public Object show(final String id, final Model model) {
        final CommandOrder commandOrder = (CommandOrder)this.commandOrderService.getById((Serializable)id);
        final Map map = BeanUtil.beanToMap(commandOrder);
        model.addAttribute("commandOrder", new CommandOrderWrapper(map).wrap());
        return this.PREFIX + "commandOrder_show.html";
    }

    @RequestMapping("/listWasteTypeBy")
    @ResponseBody
    public Object listWasteTypeBy(Long binsId){
        Bins bins = this.binsService.getById(binsId);
        String wasteTypes = bins.getWasteTypes();
        List<String> typeIdStrList = StrUtil.split(wasteTypes, ',');
        Long[] typeIdArr = Convert.toLongArray(typeIdStrList);
        QueryWrapper<WasteType> wasteTypeQueryWrapper  = new QueryWrapper<>();
        wasteTypeQueryWrapper.lambda().in(WasteType::getId,typeIdArr);
        List<WasteType> list = this.wasteTypeService.list(wasteTypeQueryWrapper);
        return list;
    }
}
